package com.cloud.translation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.cloud.translation.Dao.DaoUtils;
import com.cloud.translation.databinding.ActivityMainBinding;
import com.cloud.translation.observable.AndroidObervable;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    private MenuItem item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setEven();
    }

    private void setEven() {
        NavController navController = Navigation.findNavController(this, R.id.nav_fragment);
        navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
            if (destination.getId() == R.id.translation_history) {
                if (item != null)
                    item.setVisible(true);
            } else if (destination.getId() == R.id.translation_home) {
                if (item != null)
                    item.setVisible(false);
            }
        });
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.translation_home, R.id.translation_history)
                .build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.bottomNavView, navController);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.history_menu, menu);
        item = menu.findItem(R.id.action_message);
        item.setVisible(false);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        new DaoUtils(this).deleteAll();
        AndroidObervable.getInstance().notifyStepChange(0);
        return super.onOptionsItemSelected(item);
    }

}